package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.User;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsStockFrozen;
import com.jy.wms.pojo.WmsStockFrozenDetailed;
import com.jy.wms.service.WmsStockFrozenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ClassName: WmsStockFrozenController
 * @Description: 库存冻结主表
 * @Author Yjs
 * @Date Fri Dec 01 09:13:53 GMT+08:00 2017
 */
@RestController
@RequestMapping("/wmsstockfrozen")
@Api(value = "/wmsstockfrozen", description = "库存冻结")
public class WmsStockFrozenController extends BaseController<WmsStockFrozenService, WmsStockFrozen> {
    /**
     * 查询库存冻结信息
     *
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = {"list"})
    @ApiOperation(value = "查询库存冻结信息", response = JSONObject.class, notes = "查询库存冻结信息")
    public @ResponseBody
    JSONObject getWmsStockFrozen(@RequestBody WmsStockFrozen entity, HttpSession session) {
        Page<WmsStockFrozen> list = new Page<WmsStockFrozen>();
        JSONObject jsonObject = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                //创建时间
                /*if (entity.getStime() != null && !entity.getStime().equals("")) {
                    String[] line = entity.getStime().split("~");
                    entity.setStime(line[0]);
                    entity.setEtime(line[1]);

                }*/
                //解冻时间
                /*if (entity.getJdtime() != null && !entity.getJdtime().equals("")) {
                    String[] line = entity.getJdtime().split("~");
                    entity.setJdtime(line[0]);
                    entity.setJdetime(line[1]);
                }*/

                entity.setHzid(hz.get(0).getHzid());
                entity.setCkid(ckid);
                list = baseService.baseSelectPage(entity);
            }
            jsonObject = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            //jsonObject = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "查询库存冻结失败", "查询库存冻结失败", "");
        }
        return jsonObject;
    }

    /*@PostMapping(value = "delete")
    @ApiOperation(value = "删除信息", response = JSONObject.class, notes = "删除信息")
    public @ResponseBody
    JsonResult deleteWmsStockFrozen(@RequestBody List<String> ids) {
        Integer flag = baseService.baseDeleteBathPrimaryKeys(ids);
        return renderSuccess("删除成功");
    }*/

    /**
     * 新增库存冻结
     *
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = "add")
    @ApiOperation(value = "新增库存冻结信息", response = JSONObject.class, notes = "新增库存冻结信息")
    public @ResponseBody
    JSONObject addWmsStockFrozen(@RequestBody WmsStockFrozen entity, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //取出当前用户
            User user = userinfo.getUser();
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                entity.setHzid(hz.get(0).getHzid());
                entity.setCkid(ckid);
                entity.setCjry(user.getName());
                baseService.baseInsert(entity);
            }
        } catch (SucessException e) {
            jsonObject = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "新增库存冻结成功", e.getMessage(), JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            jsonObject = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "新增库存冻结失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "新增库存冻结失败", "新增库存冻结失败", "");
        }
        return jsonObject;
    }

    /**
     * 查询可冻结库存货品
     * 选择库存的库位类型只能为“0存储库位”、“2箱拣货位”、“1件拣货位”、“3箱/件拣货位”
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = "searchHp")
    @ApiOperation(value = "查询可冻结库存货品", response = JSONObject.class, notes = "查询可冻结库存货品")
    public @ResponseBody
    JSONObject searchHp(@RequestBody WmsStockFrozenDetailed entity, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        Page<WmsStockFrozenDetailed> list = new Page<WmsStockFrozenDetailed>();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                //创建时间
                if (entity.getScrq_start() != null && !entity.getScrq_start().equals("")) {
                    String[] line = entity.getScrq_start().split("~");
                    entity.setScrq_start(line[0]);
                    entity.setScrq_end(line[1]);
                }

                entity.setHzid(hz.get(0).getHzid());
                entity.setCkid(ckid);
                list = baseService.searchHp(entity);
            }
            jsonObject = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            //jsonObject = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "查询可冻结库存货品失败", e.getMessage(), "");
        }
        return jsonObject;
    }

    /**
     * 查询库存冻结信息
     *
     * @param entity
     * @return
     */
    @PostMapping(value = "searchFrozenMx")
    @ApiOperation(value = "解冻专用查询库存冻结明细信息", response = JSONObject.class, notes = "解冻专用分页查询库存冻结明细信息{\"id\":\"冻结主表ID)\"}")
    public @ResponseBody
    JSONObject searchFrozenMx(@RequestBody WmsStockFrozen entity) {
        JSONObject jsonObject = new JSONObject();
        Page<WmsStockFrozenDetailed> list = new Page<WmsStockFrozenDetailed>();
        try {
            list = baseService.searchFrozenMx(entity);
            jsonObject = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            //jsonObject = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "查询可冻结库存货品失败", e.getMessage(), "");
        }
        return jsonObject;
    }

    /**
     * 库存冻结明细解冻
     *
     * @param wmsStockFrozen
     * @return
     */
    @PostMapping(value = "stockThaw")
    @ApiOperation(value = "库存冻结明细解冻", response = JSONObject.class, notes = "库存冻结明细解冻")
    public @ResponseBody
    JSONObject stockThaw(@RequestBody WmsStockFrozen wmsStockFrozen, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //仓库编号
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            //货主信息
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = null;
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            String name = userinfo.getUser().getName();
            baseService.stockThaw(wmsStockFrozen, name, ckid, hzid);
        } catch (SucessException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "库存冻结明细解冻成功", "", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "库存冻结明细解冻失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "库存冻结明细解冻失败", e.getMessage(), "");
        }
        return json;
    }

    /**
     * 导出库存冻结信息
     *
     * @param response
     * @param entity
     * @param session
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "导出库存冻结信息", response = JSONObject.class, notes = "导出库存冻结信息")
    @PostMapping(value = "exportWmsStockFrozen")
    public JSONObject exportWmsStockFrozen(HttpServletResponse response, @RequestBody WmsStockFrozen entity, HttpSession session) throws IOException {
        String fileName = "库存冻结信息";
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        entity.setCkid(ckid);
        //货主信息
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        //创建时间
        if (entity.getStime() != null && !entity.getStime().equals("")) {
            String[] line = entity.getStime().split("~");
            entity.setStime(line[0]);
            entity.setEtime(line[1]);
        }
        //解冻时间
        if (entity.getJdtime() != null && !entity.getJdtime().equals("")) {
            String[] line = entity.getJdtime().split("~");
            entity.setJdtime(line[0]);
            entity.setJdetime(line[1]);
        }
        entity.setHzid(hzid);
        List<WmsStockFrozen> wmsStockFrozenList = baseService.exportWmsStockFrozen(entity);
        //Export.exportAggregate(response,wmsStockFrozenList,fileName);
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 文件名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("库存冻结信息", "UTF-8") + ".xls");
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsStockFrozen.class, wmsStockFrozenList);
        workbook.write(response.getOutputStream());
        return null;
    }
    /*@GetMapping(value = "getWmsStockFrozen/{id}")
    @ApiOperation(value = "查看单个信息", response = JSONObject.class, notes = "GET查看单个信息")
    public @ResponseBody
    WmsStockFrozen getWmsStockFrozenById(@PathVariable("id") String id) {
        WmsStockFrozen entity = baseService.baseSelectByPrimaryKey(id);
        return entity;
    }

    @PostMapping(value = "edit")
    @ApiOperation(value = "编辑信息", response = JSONObject.class, notes = "编辑信息")
    public @ResponseBody
    JsonResult editWmsStockFrozen(@RequestBody WmsStockFrozen entity) {
        Integer flag = baseService.baseUpdateByPrimaryKeySelective(entity);
        return renderSuccess("修改成功");
    }*/

    /**
     * 查询冻结单号
     *
     * @return
     */
    @PostMapping(value = "/searchDjdh")
    @ApiOperation(value = "冻结单号 冻结单号", response = JSONObject.class)
    public @ResponseBody
    JSONObject searchDjdh() {
        JSONObject jsonObject = new JSONObject();
        try {
            String djdh = baseService.searchDjdh();
            jsonObject.put("djdh", djdh);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

}
